# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openvdb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.0.0
pkgrel=3
pkgdesc="OpenVDB is an open source C++ library comprising a novel hierarchical data structure and a large suite of tools for the efficient storage and manipulation of sparse volumetric data discretized on three-dimensional grids (mingw-w64)"
arch=('any')
url="http://www.openvdb.org/"
license=("MPL 2.0")
depends=("${MINGW_PACKAGE_PREFIX}-blosc"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
checkdepends=("${MINGW_PACKAGE_PREFIX}-cppunit")
options=('!strip' 'buildflags' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/AcademySoftwareFoundation/openvdb/archive/v${pkgver}.tar.gz
        001-build-fixes.patch
        002-fix-find-cppunit.patch
        003-msvc-mingw.patch
        004-python-module-ext.patch
        609b97a05b01be22ecafe6f66fe187abb800657f.patch
        af0672f73140edd0bf489bcac6dd624677ac241c.patch
        895974a3a913da4b26efaa2128f0674338f76f71.patch)
sha256sums=('97bc8ae35ef7ccbf49a4e25cb73e8c2eccae6b235bac86f2150707efcd1e910d'
            '6e5e5b5d9130107bf8a7de4101eedc1fe2e737fc01cbe3a5d750e2b57ef10385'
            '82f1a627aaabb7bf3168998a5914fd9379e6c035a9106994523bf138b12fd0a8'
            '83c82e16f97439c953b2fb3cfb6cadda3912849bdf80e79d523ef259838aee17'
            '22d486b0aae8945cba65e0b3e5382157ba46f8db8fc042c8d157ec5b8df7674d'
            'b6524dfaae7a7ce61df62010e50179032d87841d993aeb3816bff27faaded871'
            '24f76c82ed6e915c1f901e9c48b17efb7a6b8dda4308c7720527a323ba9b0237'
            'a316de64eaf08fab891b253a9ffa9573898e73cd0cf8b5c20685d6a6db8686b5')

prepare() {
  cd ${srcdir}/openvdb-${pkgver}
  patch -p1 -i ${srcdir}/001-build-fixes.patch
  patch -p1 -i ${srcdir}/002-fix-find-cppunit.patch
  patch -p1 -i ${srcdir}/003-msvc-mingw.patch

  # Upstream patches
  patch -p1 -i ${srcdir}/609b97a05b01be22ecafe6f66fe187abb800657f.patch
  patch -p1 -i ${srcdir}/af0672f73140edd0bf489bcac6dd624677ac241c.patch
  patch -p1 -i ${srcdir}/895974a3a913da4b26efaa2128f0674338f76f71.patch

  patch -p1 -i ${srcdir}/004-python-module-ext.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  CFLAGS+=" -D_USE_MATH_DEFINES"
  CXXFLAGS+=" -D_USE_MATH_DEFINES"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_BLOSC=ON \
    -DUSE_EXR=ON \
    -DOPENVDB_CORE_SHARED=ON \
    -DOPENVDB_BUILD_PYTHON_MODULE=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ${extra_config} \
    ../openvdb-${pkgver}

  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/openvdb-${pkgver}/openvdb/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
